package cn.ljy.authority.config.security;

import cn.ljy.authority.model.dto.SysUserAuthority;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class UserUtil {

	/**
	 * 获取当前登录账户信息
	 * 说明：从spring security中读取
	 * @return
	 */
	public static SysUserAuthority getLoginUser() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication != null) {
			if (authentication instanceof AnonymousAuthenticationToken) {
				return null;
			}
			if (authentication instanceof UsernamePasswordAuthenticationToken) {
				return (SysUserAuthority) authentication.getPrincipal();
			}
		}
		return null;
	}

}
